

import HeadPrefab from "../../component/HeadPrefab";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewMathTop extends ViewBase {

    private tabIndex: number = 0

    private gameTypeConfig: Array<number> = [
        GameConfig.Game_Type.ddz,
        GameConfig.Game_Type.jianziding,
        GameConfig.Game_Type.dazhonger_mj
    ]

    private topList: Array<object> = []
    private myTop: object

    private itemNode: cc.Node;
    private myTopItem: cc.Node

    onEnable(): void {
        super.onEnable()
        this.itemNode = this.findChild(this.node, "scrollView/view/content/itemNode")
        this.itemNode.active = false
        this.myTopItem = this.findChild(this.node, "myTopItem")
        this.refreshTab();

        this.getInfo()
        this.refresh()
    }

    private getInfo() {
        // SocketClient.req_match_top(this.gameTypeConfig[this.tabIndex], (data: object) => {
        //     this.topList = data["topList"]
        //     this.myTop = data["myTop"]
        //     this.refresh()
        // })
        SocketClient.req_jindou_top((data: object) => {
            Logger.log("getInfo=", data)
            this.topList = data["topList"]
            // this.myTop = data["myTop"]
            this.refresh()
        })
    }


    private refreshTab() {
        // for (let i = 0; i < this.gameTypeConfig.length; i++) {
        //     let btnTopTab: cc.Node = this.findChild(this.node, "btnTopTab" + i)
        //     if (i == this.tabIndex) {
        //         btnTopTab.getComponent(cc.Sprite).spriteFrame = btnTopTab.getComponent(SpriteFrameComponent).spriteFrameList[1]
        //     } else {
        //         btnTopTab.getComponent(cc.Sprite).spriteFrame = btnTopTab.getComponent(SpriteFrameComponent).spriteFrameList[0]
        //     }
        //     UIHelper.onAddEvent(btnTopTab, () => {
        //         this.tabIndex = i
        //         this.refreshTab()
        //         this.getInfo()
        //     })
        // }
    }

    private refresh() {
        for (let i = 0; i < this.itemNode.parent.childrenCount; i++) {
            this.itemNode.parent.children[i].active = false
        }
        let myTopIndex: number = -1
        for (let i = 0; i < this.topList.length; i++) {
            let data: object = this.topList[i]
            let tempNode: cc.Node = this.itemNode.parent.children[i]
            if (!tempNode) {
                tempNode = cc.instantiate(this.itemNode)
                tempNode.parent = this.itemNode.parent
            }
            tempNode.active = true
            this.refreshItem(tempNode, data, i + 1)
            if (data["userid"] == UserModel.instance.userId) {
                myTopIndex = i + 1
            }
        }
        this.refreshItem(this.myTopItem, UserModel.instance, myTopIndex)
    }

    private refreshItem(itemNode: cc.Node, data: object, topIndex: number) {
        Logger.log("refreshItem---", data, topIndex)
        let lbTop: cc.Label = this.getChild(itemNode, "lbTop", cc.Label)
        let topIcon: cc.Node = this.getChild(itemNode, "topIcon")
        let lbName: cc.Label = this.getChild(itemNode, "lbName", cc.Label)
        let lbUserId: cc.Label = this.getChild(itemNode, "lbUserId", cc.Label)
        let lbScore: cc.Label = this.getChild(itemNode, "lbScore", cc.Label)
        let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
        let tiaoIcon: cc.Node = this.findChild(itemNode, "tiaoIcon")
        tiaoIcon.active = false

        lbUserId.string = ""
        lbTop.node.active = false
        topIcon.active = false
        if (topIndex == -1) {
            lbTop.string = ">100"
            lbTop.node.active = true
        } else if (topIndex <= 3) {
            lbTop.string = ""
            topIcon.active = true
            tiaoIcon.active = true
            tiaoIcon.getComponent(cc.Sprite).spriteFrame = tiaoIcon.getComponent(SpriteFrameComponent).spriteFrameList[topIndex - 1]
            topIcon.getComponent(cc.Sprite).spriteFrame = topIcon.getComponent(SpriteFrameComponent).spriteFrameList[topIndex - 1]
        } else {
            lbTop.node.active = true
            tiaoIcon.active = true
            tiaoIcon.getComponent(cc.Sprite).spriteFrame = tiaoIcon.getComponent(SpriteFrameComponent).spriteFrameList[3]
        }


        if (data) {
            lbName.string = data["nick_name"]
            lbScore.string = UIHelper.formatCoin(data["coins"])
            head.setHead(data["headimg"], data["lv"])
            lbTop.string = topIndex + ""
            lbUserId.string = "ID:" + data["userid"]

        } else {
            lbName.string = UserModel.instance.userName
            lbScore.string = 0 + ""
            head.setHead(null, 0)
        }


    }
}
